package com.uottawa.library.monitoring;

import java.util.Date;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;

import com.uottawa.library.domain.Loan;
import com.uottawa.library.domain.TransactionLogger;
import com.uottawa.library.domain.User;
import com.uottawa.library.service.SystemMonitoringService;

@Aspect
public class SystemMonitoring {
	
	private SystemMonitoringService service = new SystemMonitoringService();
	
	@AfterReturning(
			pointcut = "execution(* com.uottawa.library.service.UserService.addUser(..))" +
					"|| execution(* com.uottawa.library.service.UserService.deleteUser(..))",
			returning = "result" )
	public void logUserActions(JoinPoint joinPoint, Object result) {
 
		if(!result.equals("success")) {
			return; // if operation didn't succeed, don;t log anything.
		}
		String actionName = joinPoint.getSignature().getName();
		Object[] args = joinPoint.getArgs();

		User user = (User) args[0];
		Date date = new Date();
		TransactionLogger logger = new TransactionLogger();
		logger.setActionDate(date);
		logger.setActionName(actionName);
		logger.setUserId(user.getUserId());
		service.logTransaction(logger);
	}
	
	@AfterReturning(
			pointcut = "execution(* com.uottawa.library.service.UserService.login(..))", returning = "result" )
	public void logUserLogin(JoinPoint joinPoint, Object result) {
 
		if(result == null) {
			return; // if operation didn't succeed, don;t log anything.
		}
		String actionName = joinPoint.getSignature().getName();
		User user = (User) result;

		int userName = user.getUserId();
		Date date = new Date();
		TransactionLogger logger = new TransactionLogger();
		logger.setActionDate(date);
		logger.setActionName(actionName);
		logger.setUserId(userName);
		service.logTransaction(logger);
	}
	@AfterReturning(
			pointcut = "execution(* com.uottawa.library.service.LoanService.borrowLoanCopy(..))" +
					"|| execution(* com.uottawa.library.service.LoanService.renewLoan(..))",
			returning = "result" )
	public void logLoanActions(JoinPoint joinPoint, Object result) {
 
		if(!result.equals("success")) {
			return; // if operation didn't succeed, don;t log anything.
		}
		String actionName = joinPoint.getSignature().getName();
		Object[] args = joinPoint.getArgs();

		User user = (User) args[1];
		Date date = new Date();
		TransactionLogger logger = new TransactionLogger();
		logger.setActionDate(date);
		logger.setActionName(actionName);
		logger.setUserId(user.getUserId());
		service.logTransaction(logger);
	}
	
	@AfterReturning(
			pointcut = "execution(* com.uottawa.library.service.LoanService.returnLoan(..))",
			returning = "result" )
	public void logLoanReturn(JoinPoint joinPoint, Object result) {
 
		if(!result.equals("success")) {
			return; // if operation didn't succeed, don;t log anything.
		}
		String actionName = joinPoint.getSignature().getName();
		Object[] args = joinPoint.getArgs();

		Loan loan = (Loan) args[0];
		Date date = new Date();
		TransactionLogger logger = new TransactionLogger();
		logger.setActionDate(date);
		logger.setActionName(actionName);
		logger.setUserId(loan.getUserId());
		service.logTransaction(logger);
	}
}
